<template>
  <div>
    <el-table
      :data="arr"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="id" label="规格编号" width="180">
      </el-table-column>
      <el-table-column prop="specsname" label="规格名称" width="180">
      </el-table-column>

      <el-table-column label="规格属性" width="180">
        <template slot-scope="scope">
          <el-tag
            :type="type.types[index]"
            v-for="(item, index) in scope.row.attrs"
            :key="item.id"
            >{{ item }}</el-tag
          >
          
        </template>
      </el-table-column>

      <el-table-column prop="type" label="状态">
        <template slot-scope="scope">
          <el-button type="primary" v-if="scope.row.status === 1"
            >启用</el-button
          >
          <el-button type="info" v-else>禁用</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" @click="bianji(scope.row.id)"
            >编辑</el-button
          >
          <el-button type="danger" @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      :page-size="pageSize"
      @current-change="changePage"
    >
    </el-pagination>
  </div>
</template>

<script>
import { reqspecsdelete } from "../../../http/api";
import { successAlert } from "../../../utils/alert";
export default {
  props: ["arr", "pageSize", "total"],
  data() {
    return {
      type: {
        types: ["success", "info", "", "warning", "danger"],
      },
    };
  },
  methods: {
    bianji(id) {
      this.$emit("info", id);
    },
    changePage(n) {
      //通知父组件修改
      this.$emit("changePage", n);
    },
    // 点了删除
    del(id) {
      this.$confirm("你确定要删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //点了确定
          reqspecsdelete({
            id: id,
          }).then((res) => {
            if (res.data.code === 200) {
              //弹成功
              successAlert(res.data.msg);
              //重新请求列表
              this.$emit("init");
            }
          });
        })
        .catch(() => {
          //点了取消
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  
};
</script>

<style>
.cell{
  text-align: center;
}
.cell span{
  margin-left: 3  px;
  margin-right: 3px ;
}
</style>
